package com.zhangwoo.service.word.analyser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

import com.meiya.common.simpledao.BaseDao;
import com.meiya.common.simpledao.Config;

public class WordAnalyser {
	private static Logger logger = Logger.getLogger(WordAnalyser.class.getName());
	private static Map<String,List<Object[]>> answers = new HashMap<String,List<Object[]>>();
	public static List<Object[]> getAnswers(String weixin_no_id){
		return answers.get(weixin_no_id);
	}
	
	public static void main(String[] args) {
		loadAnswers();
		startWebCenter();
	}
	
	/**
	 * 将答案加载至内存中
	 */
	public static void loadAnswers(){
		try {
			answers.clear();
			List<Object[]> res=new BaseDao().query(Config.getProps("sql"));
			logger.info("load sql is : "+Config.getProps("sql"));
			
			for (Object[] o : res) {
				if(!answers.containsKey(o[2])){
					answers.put(o[2].toString(), new ArrayList<Object[]>());
				}
				answers.get(o[2].toString()).add(o);
			}
		} catch (Exception e) {
			logger.error("load answers error",e);
		}
	}
	
	public static void startWebCenter() {
		Server server = new Server(40000);

		WebAppContext webApp = new WebAppContext();
		webApp.setContextPath("/zwservice");
		if(new java.io.File("src/main/webapp").exists())
			webApp.setWar("src/main/webapp");
		else
			webApp.setWar(".");
		server.setHandler(webApp);

		try {
			server.start();
		} catch (Exception e) {
			logger.error("Web 服务启动失败  ", e);
		}
	}

}
